package edu.hainnu.niit.service.Impl;

import edu.hainnu.niit.dao.NewsDao;
import edu.hainnu.niit.model.News;
import edu.hainnu.niit.model.PageBean;
import edu.hainnu.niit.service.INewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @package_name edu.hainnu.niit.service.Impl
 * @auther GGB
 * @date 2021/12/12
 * @time 14:18
 */
@Service
public class NewsServiceImpl implements INewsService {
    @Resource
    NewsDao newsDao;

    @Override
    public List<News> LatestConsultation(Integer pageSize) {
        List<News> newsList = newsDao.LatestConsultation(pageSize);
        return newsList;
    }

    @Override
    public List<News> IndustryDynamics(Integer pageSize) {
        List<News> newsList = newsDao.IndustryDynamics(pageSize);
        return newsList;
    }

    @Override
    public List<News> PopularConsultation(Integer pageSize) {
        List<News> newsList= newsDao.PopularConsultation(pageSize);
        return newsList;
    }

    @Override
    public PageBean<News> queryNewsService(Integer currentPage, Integer pageSize,String sname) {
        PageBean<News> pageBean =new PageBean<News>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int Count = newsDao.queryNewsTotal(sname);
        pageBean.setTotalCount(Count);

        int totalPage = Count % pageSize == 0 ? Count / pageSize :(Count / pageSize) + 1 ;
        pageBean.setTotalPage(totalPage);

        currentPage=(currentPage-1)*pageSize;
        pageBean.setList(newsDao.queryAllNews(currentPage,pageSize,sname));
        return pageBean;
    }

    @Override
    public PageBean<News> queryTypeNewsService(Integer currentPage, Integer pageSize, Integer Type, String sname) {
        PageBean<News> pageBean =new PageBean<News>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        int Count = newsDao.queryTypeNewsTotal(Type,sname);
        pageBean.setTotalCount(Count);

        int totalPage = Count % pageSize == 0 ? Count / pageSize :(Count / pageSize) + 1 ;
        pageBean.setTotalPage(totalPage);

        currentPage=(currentPage-1)*pageSize;
        pageBean.setList(newsDao.queryTypeNews(currentPage,pageSize,Type,sname));
        return pageBean;
    }

    @Override
    public News queryNewsByIDService(Integer id) {
        newsDao.NewsCountadd(id);
        return newsDao.queryNewsByID(id);
    }
}
